package com.tan.steamship.controller;

import com.tan.steamship.common.Result;
import com.tan.steamship.service.ShoppingCartService;
import com.tan.steamship.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 购物车
 * @author: longshifu
 * @time: 2025/2/9 11:31
 */
@RestController
@RequestMapping("/cart/v1")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 根据用户获取购物车信息
     * @param userId
     * @return
     */
    @GetMapping("/user/{userId}")
    public Result cart(@PathVariable String userId) {
        List<CartVo> carts = shoppingCartService.getCartByUserId(userId);
        return Result.success(carts);
    }

    /**
     * 添加购物车
     * @param routeId
     * @param userId
     * @return
     */
    @PostMapping("/add/{routeId}/{userId}")
    public Result cart(@PathVariable Integer routeId, @PathVariable Integer userId) {
        return shoppingCartService.addCart(routeId, userId);
    }

    @DeleteMapping("/delete/{cartId}")
    public Result deleteCart(@PathVariable String cartId) {
        shoppingCartService.deleteCart(cartId);
        return Result.success("删除成功");
    }

    @PutMapping("/update/num/{cartId}/{num}")
    public Result updateNum(@PathVariable String cartId,  @PathVariable String num) {
        shoppingCartService.updateCartNum(cartId, num);
        return Result.success("更新成功");
    }

}
